<?php
use \Workerman\Autoloader;

require_once __DIR__ . '/../../../../../Workerman/Autoloader.php';
require_once __DIR__.'/../../../Config/Const.php';
Autoloader::setRootPath(__DIR__.'/../../..');

$page_groupId = GROUP_TREE_MNG;
$page_pageId = PAG_SPECIES_LIST;

require "../framepages/moudle_base.php";

$needle = $_GET['needle'];

$gg_list = \Data\Tree_gg_DAO::getList();

$result = 'normal';
$mode = $_POST['mode'];
if ($mode == 'species_dd') {
    $species_name = $_POST['species_name'];
    $dw = $_POST['dw'];
    $gg = $_POST['gg'];
    $species_same_name_record = \Data\Tree_species_DAO::getRecordByName($species_name);
    if (!$species_name) {
        $result = 'unset_name';
    } elseif (!$dw) {
        $result = 'unset_dw';
    } elseif ($species_same_name_record) {
        $result = 'err_same_name';
    } else {
        $species_record = new \Data\Tree_species(array());
        $species_record->setSPECIES_NAME($species_name);
        $species_record->setGG_ID($gg);
        $species_record->setDW($dw);
        $species_id = \Data\Tree_species_DAO::insert($species_record);

        echo "
        <script>
            alert('添加成功');
        </script>
        ";
    }
}

$mode_act = $_GET['mode_act'];
if ($mode_act == 'delete_species') {

    $species_id = $_GET['species_id'];
    $group_list = \Data\Tree_group_DAO::getListBySpecies_id($species_id);
    if ($group_list) {
        echo "
        <script>
            alert('不能删除，该品种下还有分组列表');
        </script>
    ";
    } else {
        $species_record = \Data\Tree_species_DAO::getRecord($species_id);
        \Data\Tree_species_DAO::delete($species_record);
        echo "
        <script>
            alert('删除成功');
        </script>
    ";
    }
}

if ($mode_act == 'edit_species') {
    $species_id = $_GET['species_id'];
    $species_name = $_GET['species_name'];
    $gg_id = $_GET['gg_id'];
    $species_record = \Data\Tree_species_DAO::getRecord($species_id);
    $species_same_name_record = \Data\Tree_species_DAO::getIdArrByName($species_name);
    if (!$species_name) {
        $result = 'unset_edit_name';
    } elseif ($species_same_name_record && !(in_array($species_id,$species_same_name_record))) {
        $result = 'err_edit_same_name';
    } else {
        $species_record->setSPECIES_NAME($species_name);
        $species_record->setGG_ID($gg_id);
        \Data\Tree_species_DAO::update($species_record);
        echo "
        <script>   
            alert('修改成功');      
        </script>
        ";
    }
}


?>
<div class="main-content">
    <div class="search-well">
        <form class="form-inline" style="margin-top: 0px;">
            模糊搜索 &nbsp;<input class="input-xlarge form-control"
                              placeholder="品种名称" id="4sStoreName" type="text" value="<?php echo $needle ?>"> &nbsp;
            <button class="btn btn-default" type="button" id="searchButton">
                <i class="fa fa-search"></i> 查找
            </button>
        </form>
    </div>
    <br />

    <div>
        <table class="table" style="word-break:break-all" style="width: 100%">
            <tr>

                <th width="9%">序号</th>
                <th width="9%">品种id</th>
                <th width="9%">品种名称</th>
                <th width="9%">规格类型</th>
                <th width="12%">查看分组</th>
                <th width="12%">修改</th>
                <th width="12%">删除</th>
            </tr>

            <?php
            $needle = $_GET['needle'];
            $page_index = $_GET['page_index'];
            if (!$page_index) {
                $page_index = 1;
            }
            $every_page_counts = 30;
            $list_start = ($page_index-1)*$every_page_counts;
            $species_list_array = \Data\Tree_species_DAO::getListByNeedle($needle,$list_start,$every_page_counts);
            $species_list = $species_list_array[1];
            $total_count = $species_list_array[0];
            $page_count = ceil($total_count/$every_page_counts);
            $page_last = $page_index-1;
            $page_next = $page_index+1;
            $i = $list_start+1;
            foreach ($species_list as $species_record) {
                $species_id = $species_record->getSPECIES_ID();
                $species_name = $species_record->getSPECIES_NAME();
                $gg_id_record = $species_record->getGG_ID();
                echo "
                        <tr>
                            <td>$i</td>
                            <td>$species_id</td>
                            <td><input type='text' id='name_$species_id' name='species_name' value='$species_name'></td>
                            <td><select id='gg_id_$species_id' name='gg_id_$species_id'>";
                foreach ($gg_list as $gg_record) {
                    $gg_id = $gg_record->getGG_ID();
                    $gg_name = $gg_record->getGG_NAME();
                    $gg_dw = $gg_record->getGG_DW();
                    $gg = $gg_name." (".$gg_dw.")";
                    if ($gg_id_record == $gg_id) {
                        echo "
                            <option selected='selected' value='$gg_id'> $gg </option>                  
                          ";
                    } else {
                        echo "
                            <option value='$gg_id'> $gg </option>                  
                          ";
                    }


                }

                echo " </select></td>                           
                            <td><button type='button' class='btn btn-danger' onclick='group_list($species_id)'>查看分组</button></td>
                            <td><button type='button' class='btn btn-danger' onclick='edit_species($species_id)'>修改</button></td>
                            <td><button type='button' class='btn btn-danger' onclick='delete_species($species_id)' >删除</button></td>                                                 
                        </tr>
                    ";
                $i++;
            }
            ?>

        </table>
    </div>
    <div style="margin-left:35%; margin-top:40px;">
        <div>
        <?php
            if ($page_last) {
                echo "            
                    <button onclick='last_page($page_last)'>上一页</button>                            
                ";
            }

            if ($page_next<=$page_count) {
                echo "         
                    <button onclick='next_page($page_next)'>下一页</button>               
                ";
            }

            if ($page_count>1) {
                    echo "
                    &nbsp;&nbsp;&nbsp;&nbsp;去往第<input type='text' name='go_page' id='go_page' size='3'>页
                <button onclick='go_page()'>GO</button>
                    ";
            }
            ?>



        </div>

        <div style="margin-left:5%; margin-top:20px;">
            第<?php echo $page_index ?>页 &nbsp;&nbsp;&nbsp;&nbsp;
            共<?php echo $page_count ?>页  &nbsp;&nbsp;&nbsp;&nbsp;共<?php echo $total_count ?>条记录
        </div>


    </div>
    <?php
    require "../framepages/footer.php";
    ?>
    <div class="main-content">
        <div>
            <span><h2>添加品种</h2></span>
        </div>
        <form action="" method="post">
            <input type="hidden" name="mode" id="mode" value='species_dd'>
            <table class="res-table">
                <tr>
                    <td width="12%">品种名称:</td>
                    <td width="20%"><input type="text" id="species_name" name="species_name" placeholder="例如：白蜡" ></td>
                    <td width="8%">规格类型:</td>
                    <td>
                        <select id='gg' name='gg'>
                            <?php
                            foreach ($gg_list as $gg_record) {
                                $gg_id = $gg_record->getGG_ID();
                                $gg_name = $gg_record->getGG_NAME();
                                $gg_dw = $gg_record->getGG_DW();
                                $gg = $gg_name." (".$gg_dw.")";

                                echo "
                            <option id='gg' name='gg' value='$gg_id'> $gg </option>                  
                            ";
                            }
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>单位：</td>
                    <td><input type="text" name="dw" placeholder="例如:株"></td>
                    <td></td>
                    <td></td>
                </tr>
            </table>
            <div style="margin-top:30px;">
                <input type="submit" class="btn btn-danger" value="提交">
            </div>
        </form>


    </div>


</div>
</div>
<script src="../lib/bootstrap/js/bootstrap.js"></script>

<script>
    function init() {
        var result = "<?php echo $result ?>";
        if (result == 'unset_name') {
            alert("添加失败，请输入品种名称");
        }
        if (result == 'unset_dw') {
            alert("添加失败，请输入单位");
        }
        if (result == 'err_same_name') {
            alert("添加失败，已存在同名称品种");
        }

        if (result == 'unset_edit_name') {
            alert("修改失败，请输入品种名称");
        }
        if (result == 'err_edit_same_name') {
            alert("修改失败，已存在同名称品种");
        }
        var mode = "<?php echo $mode ?>";
        var mode_act = "<?php echo $mode_act ?>";
        if (mode || mode_act) {
            window.location.href = 'species_list.php';
        }
    }



    $('#searchButton').click(function() {
            needle = $('#4sStoreName').val();
            url = "?needle="+needle;
            window.location.href = url;
        }
    );


    function last_page(page_last) {
        needle = $('#4sStoreName').val();
        url = "?page_index="+page_last+"&needle"+needle;
        window.location.href = url;
    }

    function next_page(page_next) {
        needle = $('#4sStoreName').val();
        url = "?page_index="+page_next+"&needle"+needle;
        window.location.href = url;
    }

    function go_page()
    {
        needle = $('#4sStoreName').val();
        url = "?page_index="+$('#go_page').val()+"&needle"+needle;
        window.location.href = url;
    }

    function group_list(species_id) {
        url = "group_list.php?species_id="+species_id;
        window.location.href = url;
    }

    function edit_species(species_id) {
        var species_name = document.getElementById('name_'+species_id).value;
        var gg_id = document.getElementById('gg_id_'+species_id).value;
        url = "?mode_act=edit_species&species_id="+species_id+"&species_name="+species_name+"&gg_id="+gg_id;
        window.location.href = url;
    }

    function delete_species(species_id) {
        if (window.confirm('确认删除吗')) {
            url = "?mode_act=delete_species&species_id="+species_id;
            window.location.href = url;
        }
    }

</script>
</body>
</html>
